package com.xian.controller;

import com.xian.pojo.SysLog;
import com.xian.service.SysLogService;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Date;

@Component
@Aspect
public class LogAop {
    @Autowired
    private SysLogService sysLogService;
    @Autowired(required = false)
    private HttpServletRequest request;
    private Date visitTime;  // 开始时间
    private Class clazz;     // 访问的类
    private Method method;   // 访问的方法
    // 前置通知
    @Before("execution(* com.xian.controller.*.*(..))")
    public void doBefore(JoinPoint jp) throws NoSuchMethodException {
        visitTime = new Date(); // 当前时间就是开始访问的时间
        String name = jp.getSignature().getName();// 访问的方法名称
        clazz = jp.getTarget().getClass(); // 具体访问的类
        Object[] args = jp.getArgs();// 获得访问方法的参数
        if (args == null || args.length ==0){
            method = clazz.getMethod(name);
        }
        else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            clazz.getMethod(name,classArgs);
        }
    }
    // 后置通知
    @After("execution(* com.xian.controller.*.*(..))")
    public void doAfter(JoinPoint jp){
        // 获取访问的时长
        long executionTime = new Date().getTime() - visitTime.getTime();
        // 获取url
        String url = "";
        if (clazz != null && method != null && clazz != LogAop.class){
            RequestMapping classAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (classAnnotation != null){
                String[] classValue = classAnnotation.value();
                //  获取方法上的url
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null){
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];
                    // 获取访问的IP 地址
                    String ip = request.getLocalAddr();
                    System.out.println(ip);
                    System.out.println(request.getRemoteAddr());
                    // 获取当前操作的用户名
                    SecurityContext context = SecurityContextHolder.getContext(); //从上下文获得当前用户
                    User user = (User) context.getAuthentication().getPrincipal();
//        User user = (User) request.getSession().getAttribute("SPRING_SECUTITY_CONTEXT");
                    String username = user.getUsername();

                    // 将日志相关信息封装到SysLog对象
                    SysLog sysLog = new SysLog();
                    sysLog.setExecutionTime(executionTime);
                    sysLog.setIp(ip);
                    sysLog.setMethod("[类名]"+clazz.getName()+"[方法名]"+method.getName());
                    sysLog.setUrl(url);
                    sysLog.setUsername(username);
                    sysLog.setVisitTime(visitTime);
                    sysLogService.save(sysLog);
                }
            }
        }

    }

}
